obs_csv <- file.path(dir_data, "obs.csv")
obs_geo <- file.path(dir_data, "obs.geojson")

# get species occurrence data from GBIF with coordinates
(res <- spocc::occ(
  query = 'Dendroctonus valens', 
  from = 'gbif', 
  has_coords = T, 
  limit = 10000))
## Searched: gbif
## Occurrences - Found: 1,766, Returned: 1,766
## Search type: Scientific
##   gbif: Dendroctonus valens (1766)
# extract data frame from result
df <- res$gbif$data[[1]] 
nrow(df) # number of rows
## [1] 1766
# convert to points of observation from lon/lat columns in data frame
obs <- df %>% 
  sf::st_as_sf(
    coords = c("longitude", "latitude"),
    crs = st_crs(4326))

readr::write_csv(df, obs_csv)
geojson_write(obs, obs_geo)
## Success! File is at myfile.geojson
## <geojson-file>
##   Path:       myfile.geojson
##   From class: geo_list
# show points on map
mapview::mapview(obs, map.types = "Esri.WorldTerrain")

**Question 1: In total, there was 1,766 global observations of Dendroctonus valens (red turpentine beetle).

**Question 2: Observation #12 was the only species located outside of North America. However, considering that the state/Province and country code are correct, I believe this is not a mistake.